# Name:    CConfigReader.py
# Date:    Sun Mar 21 19:11:46 CET 2010
# Purpose: Load and parse config file and get variables/valus for given section
# Def:     ConfigReader(fileName)
# Inputs:  filename - the name of config file

import ConfigParser

class CConfigReader():
	'''
	'' Load and parse config file and get variables/values for given section
	'''
	__cfgFilename = None
	__oCfgParser = None # raw config parser object

#---------------------------------------------------------------------------------------------------

	def __init__(self, filename):
		self.__cfgFilename = filename

#---------------------------------------------------------------------------------------------------

	def getCfgFilename(self):
		return self.__cfgFilename

#---------------------------------------------------------------------------------------------------

	def setCfgFilename(self, filename):
		self.__cfgFilename = filename

#---------------------------------------------------------------------------------------------------

	def load(self):
		self.__oCfgParser = ConfigParser.RawConfigParser()
		self.__oCfgParser.read(self.getCfgFilename())

#---------------------------------------------------------------------------------------------------

	def getSettings(self, section = None):
		'''
		'' return all items in the appropriate section. If section is None return the list of sections
		'''
		# TODO: raise exception if oCfgParser is None
		if self.__oCfgParser is None:
			return None

		if section is None:
			return self.__oCfgParser.sections()
		else:
			return self.__oCfgParser.items(section) 

#---------------------------------------------------------------------------------------------------

	def getValue(self, section, option):
		'''
		'' return value of option which is located in the appropriate section
		'''
		# TODO: raise exception if section/option does not exist
		return self.__oCfgParser.get(section, option)

####################################################################################################
